home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2209 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.5 KB  |  45 lines

  1. Path: news.th-darmstadt.de!news
  2. From: Enno Sandner <enno@intellektik.informatik.th-darmstadt.de>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Using a Base constructor for a derived class
  5. Date: Tue, 16 Jan 1996 15:00:07 +0100
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Message-ID: <30FBAF67.7DE14518@intellektik.informatik.th-darmstadt.de>
  8. References: <4ddui9$a14@felix.cc.gatech.edu>
  9. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b5 (X11; I; SunOS 4.1.3 sun4m)
  14.  
  15. Matthew W. Culbreth wrote:
  16. > I have a base class with a constructor.  This class is never used
  17. > in my application.  I have three classes derived from this class.
  18. > When I create an instant of one of the derived class, I want to use the
  19. > constructor from the base class.  The compiler is stating that it can't find
  20. > the constructor for the arguments passed for any of the derived classes.
  21. > Are constructors inherited the same as member functions?
  22.  
  23. If a base-class doesn't provide a default-ctor (ie. the ctor that
  24. takes no arguments) you have to use the initializer list in the
  25. derived class to call the appropriate base-class ctor. Example:
  26.  
  27.     class Base {
  28.        public:
  29.              Base(int i) { ...}
  30.         };
  31.  
  32.         class Der : public Base {
  33.            public:
  34.              Der() : Base(4711) {}
  35.         };
  36.  
  37. The base-class needs an integer argument to get properly initialized
  38. and the initializer list of the derived class allows you to pass the
  39. necessary value.
  40.  
  41.     Enno
  42.